package com.moleq.itemsetting.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.moleq.mgpos.activities.R;
import com.moleq.models.ItemTareSetting;

public class ItemTareSelectionAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<ItemTareSetting> Source;
	private int selectedRow = 0;
	public ItemTareSelectionAdapter(Context context,List<ItemTareSetting> Source){
		this.mInflater = LayoutInflater.from(context);
		this.Source = Source;
	}
	
	public void setSource(List<ItemTareSetting> Source){
		this.Source = Source;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Source.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return Source.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.tare_row, null);
			holder.tv_itemsname = (TextView) convertView.findViewById(R.id.tv_tare_name);
			holder.tv_itemsvalue = (TextView) convertView.findViewById(R.id.tv_tare_value);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_itemsname.setText(Source.get(position).getDesc());
		holder.tv_itemsvalue.setText(Source.get(position).getValue());
		
		if (position == selectedRow) {
			holder.tv_itemsname.setBackgroundColor(Color.rgb(255,219,16));
			holder.tv_itemsvalue.setBackgroundColor(Color.rgb(255,219,16));
		} else {
			holder.tv_itemsname.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_itemsvalue.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		
		return convertView;
	}
	public final class ViewHolder{
		public TextView tv_itemsname;
		public TextView tv_itemsvalue;
	}
	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}
}
